home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-13 | 21.5 KB | 881 lines | [TEXT/CWIE] |
-
- //===============================================================================
- //
- // ScrapPicker.c
- //
- // Copyright: © 1997 by Apple Computer, Inc., all rights reserved.
- // by john calhoun & David Hayward
- //
- //===============================================================================
-
-
- #include <ColorPickerComponents.h>
- #include <Gestalt.h>
- #include <Resources.h>
- #include <Scrap.h>
- #include <TextUtils.h>
- #include "ScrapPicker.h" // <-- Project interfaces.
- #include "PickerCommon.h"
-
-
- #define ASSUME_CURR_ORIG_PROFS_SAME 1
-
-
- //===================================================================== Functions
- //--------------------------------------------------------------------- doPickerOpen
-
- pascal ComponentResult doPickerOpen (PickerStorageHandle storage, ComponentInstance self)
- {
- PickerStoragePtr pStorage;
- OSErr theErr;
-
- theErr = noErr;
-
- // Allocate handle for picker globals.
- storage = (PickerStorageHandle)HappiNewHandleClear(sizeof(PickerStorage), &theErr);
- require(storage, bail);
-
- SetComponentInstanceStorage(self, (Handle)storage);
-
- HLock((Handle)storage);
- pStorage = *storage;
-
- // Store reference to self (this instance).
- pStorage->myself = self;
-
- // Initialize a couple fields.
- pStorage->realPicker = false;
- pStorage->baseItem = 1;
-
- HUnlock((Handle)storage);
-
- bail:
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doPickerClose
-
- pascal ComponentResult doPickerClose (PickerStorageHandle storage, ComponentInstance self)
- {
- #pragma unused (self)
- PickerStoragePtr pStorage;
- char wasState;
-
- // If storage is nil, return.
- if (!storage)
- return noErr;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- // If we were fully set up properly (see PickerOpen()).
- if (pStorage->realPicker)
- {
- // Handle pattern disposal.
- if (pStorage->useColorPats)
- {
- if (pStorage->newColorPat)
- DisposePixPat(pStorage->newColorPat);
- if (pStorage->origColorPat)
- DisposePixPat(pStorage->origColorPat);
- }
- else
- {
- if (pStorage->newColorPat)
- DisposeHandle((Handle)pStorage->newColorPat);
- if (pStorage->origColorPat)
- DisposeHandle((Handle)pStorage->origColorPat);
- }
- }
-
- // Remove globals storage.
- HSetState((Handle)storage, wasState);
- DisposeHandle((Handle)storage);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerCanDo
-
- pascal ComponentResult doPickerCanDo (PickerStorageHandle storage, SInt16 selector)
- {
- #pragma unused (storage)
-
- if ((selector >= kComponentTargetSelect) && (selector <= kComponentOpenSelect))
- return true;
-
- if ((selector >= kInitPicker) && (selector <= kExtractHelpItem))
- return true;
-
- return false;
- }
-
- //--------------------------------------------------------------------- doPickerVersion
-
- pascal ComponentResult doPickerVersion (PickerStorageHandle storage)
- {
- #pragma unused (storage)
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerRegister
-
- pascal ComponentResult doPickerRegister (PickerStorageHandle storage)
- {
- #pragma unused (storage)
-
- return noErr; // always register
- }
-
- //--------------------------------------------------------------------- doPickerSetTarget
-
- pascal ComponentResult doPickerSetTarget (PickerStorageHandle storage,
- ComponentInstance topOfCallChain)
- {
- #pragma unused (storage, topOfCallChain)
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerInit
- // One thing to note, the baseItem is already set at this time. So, when
- // attempting to access dialog items, the index of these will be offset
- // by the amount of baseItem. The local function GetItemRect() takes that into
- // account.
-
- pascal ComponentResult doPickerInit (PickerStorageHandle storage, PickerInitData* data)
- {
- GDHandle thisDevice;
- PickerStoragePtr pStorage;
- char wasState;
- OSErr theErr;
- SInt16 resFile;
-
- theErr = noErr;
- resFile = 0;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- // Open our resource fork.
- resFile = OpenComponentResFile((Component)pStorage->myself);
- require_action(resFile > 0, fail, theErr = cantLoadPicker;);
-
- // Init some fields - port, etc..
- GetPort(&pStorage->port);
- pStorage->flags = data->flags;
- pStorage->visible = false;
- pStorage->active = true;
- pStorage->pictNumber = -1;
-
- // Set up color depth data.
- thisDevice = GetGDevice();
- pStorage->depth = (*((*thisDevice)->gdPMap))->pixelSize;
- pStorage->useColorPats = (pStorage->depth > 1);
-
- // Allocate the pixpats.
- if (pStorage->useColorPats)
- {
- pStorage->newColorPat = NewPixPat();
- pStorage->origColorPat = NewPixPat();
- }
- else
- {
- pStorage->newColorPat = (PixPatHandle)NewHandle(sizeof(Pattern));
- pStorage->origColorPat = (PixPatHandle)NewHandle(sizeof(Pattern));
- }
- require_action(pStorage->newColorPat && pStorage->origColorPat, fail, theErr = cantLoadPicker;);
-
- // We got through this far, so we're a real picker now!
- pStorage->realPicker = true;
-
- theErr = CloseComponentResFile(resFile);
- check(theErr == noErr);
-
- HSetState((Handle)storage, wasState);
-
- return theErr;
-
- fail:
-
- if (pStorage->useColorPats)
- {
- if (pStorage->newColorPat)
- DisposePixPat(pStorage->newColorPat);
- if (pStorage->origColorPat)
- DisposePixPat(pStorage->origColorPat);
- }
- else
- {
- if (pStorage->newColorPat)
- DisposeHandle((Handle)pStorage->newColorPat);
- if (pStorage->origColorPat)
- DisposeHandle((Handle)pStorage->origColorPat);
- }
-
- // Make sure we got a reasonable error code.
- if (theErr == noErr)
- theErr = cantLoadPicker;
-
- // Don't check the error here - we are already in an error state.
- if (resFile > 0)
- (void) CloseComponentResFile(resFile);
-
- HSetState((Handle)storage, wasState);
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doPickerTestGraphicsWorld
- // We use this function to see if Color Picker 2.1 is around. SInce we
- // rely on the new Color Picker's eyedropper functionality, we return an
- // error if 2.1 is not available. The effect of this is that the Color
- // Picker Manager (CPM) will grey out (disable) our picker's icon.
-
- pascal ComponentResult doPickerTestGraphicsWorld (PickerStorageHandle storage, PickerInitData* data)
- {
- #pragma unused (storage, data)
- SInt32 value;
- OSErr theErr;
-
- theErr = Gestalt(gestaltColorPickerVersion, &value);
- if (theErr != noErr)
- goto bail;
-
- theErr = (value < 0x0210);
-
- bail:
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doPickerGetDialog
-
- pascal ComponentResult doPickerGetDialog (PickerStorageHandle storage)
- {
- PickerStoragePtr pStorage;
- char wasState;
- OSErr theErr;
- DialogPtr theDialog;
- GrafPtr wasPort;
- SInt16 resFile;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- resFile = OpenComponentResFile((Component)pStorage->myself);
- require(resFile > 0, finish);
-
- HSetState((Handle)storage, wasState);
-
- theDialog = 0L;
- theDialog = GetNewDialog(kPickerDialog, 0L, (WindowRef)-1);
-
- DetachResource((Handle)((WindowRecord *)theDialog)->windowDefProc);
-
- theErr = CloseComponentResFile(resFile);
- check(theErr == noErr);
-
- if (theDialog)
- {
- GetPort(&wasPort);
- SetPort((GrafPtr)theDialog);
-
- // Set up the origin of the dialog.
- SetOrigin(-70, -40);
- SetPort(wasPort);
- }
-
- finish:
-
- return (SInt32)theDialog;
- }
-
- //--------------------------------------------------------------------- doPickerGetItemList
-
- pascal ComponentResult doPickerGetItemList (PickerStorageHandle storage)
- {
- OSErr theErr;
- Handle theItems;
- SInt16 resFile;
-
- theItems = 0L;
-
- resFile = OpenComponentResFile((Component)(*storage)->myself);
- require(resFile > 0, finish);
-
- // Note: DO NOT call Happi version of this call.
- // It doesn't work if the DITL is loaded into temp mem.
- theItems = GetResource('DITL', kPickerDITL);
- check(theItems);
-
- if (theItems)
- DetachResource(theItems);
-
- theErr = CloseComponentResFile(resFile);
- check(theErr == noErr);
-
- finish:
-
- return (SInt32)theItems;
- }
-
- //--------------------------------------------------------------------- doPickerGetColor
-
- pascal ComponentResult doPickerGetColor (PickerStorageHandle storage, ColorType whichColor,
- PMColorPtr color)
- {
- if (whichColor == kNewColor)
- *color = (*storage)->currPMColor;
- else
- *color = (*storage)->origPMColor;
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetColor
-
- pascal ComponentResult doPickerSetColor (PickerStorageHandle storage, ColorType whichColor,
- PMColorPtr color)
- {
- if (whichColor == kNewColor)
- {
- (*storage)->wasColor = (*storage)->currPMColor;
- (*storage)->currPMColor = *color;
- }
- else
- (*storage)->origPMColor = *color;
-
- // If the picker is already visible, we need to update everything.
- if ((*storage)->visible)
- {
- SetColorPattern(storage, whichColor);
- DrawColorEditor(storage, kDrawNew + kDrawOrig);
- }
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerEvent
-
- pascal ComponentResult doPickerEvent (PickerStorageHandle storage, EventData* data)
- {
- OSErr theErr;
- char wasState;
-
- theErr = noErr;
- data->handled = false;
- data->action = kDidNothing;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
-
- if (data->event)
- {
- switch (data->event->what)
- {
- case nullEvent:
- theErr = DoIdle(storage, data);
- break;
-
- case mouseDown:
- theErr = DoMouseDown(storage, data);
- break;
-
- case keyDown:
- case autoKey:
- theErr = DoKeyDown(storage, data);
- break;
-
- case keyUp:
- theErr = DoKeyUp(storage, data);
- break;
-
- case activateEvt:
- if (data->event->modifiers & activeFlag)
- ActivatePicker(storage);
- else
- DeactivatePicker(storage);
- break;
-
- case osEvt:
- if (((char *)&data->event->message)[0] & suspendResumeMessage)
- {
- if ( (data->event->message & resumeFlag) &&
- (FrontWindow() == (*storage)->port) )
- ActivatePicker(storage);
- else
- DeactivatePicker(storage);
- }
- break;
-
- default:
- check(true);
- break;
- }
- }
- else
- {
- // we got a forecast event...
- switch (data->forcast)
- {
- case kDialogAccept:
- break;
- // The default case is okay - it probably means we're switching out
- // of the picker and into another picker.
- default:
- break;
- }
- }
-
- HSetState((Handle)storage, wasState);
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doPickerEdit
-
- pascal ComponentResult doPickerEdit (PickerStorageHandle storage, EditData* data)
- {
- PickerStoragePtr pStorage;
- char wasState;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- switch (data->theEdit)
- {
- case kUndo:
- (void) doPickerSetColor(storage, kNewColor, &((*storage)->wasColor));
- data->action = kColorChanged;
- data->handled = true;
- break;
-
- default:
- data->action = kDidNothing;
- data->handled = false;
- break;
- }
-
- HSetState((Handle)storage, wasState);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetVisibility
-
- pascal ComponentResult doPickerSetVisibility (PickerStorageHandle storage, Boolean visible)
- {
- // Do nothing if state is the same.
- if ((*storage)->visible == visible)
- return noErr;
-
- // Note new state.
- (*storage)->visible = visible;
-
- if (visible)
- SetColorPattern(storage, kOriginalColor);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerDisplay
-
- pascal ComponentResult doPickerDisplay (PickerStorageHandle storage)
- {
- if (!(*storage)->visible)
- return noErr;
-
- DrawColorEditor(storage, kDrawEverything);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerItemHit
-
- pascal ComponentResult doPickerItemHit (PickerStorageHandle storage, ItemHitData* data)
- {
- PickerStoragePtr pStorage;
- char wasState;
- OSErr theErr;
- Rect iBox;
- Handle theItem;
- SInt16 iType, resFile;
-
- // Assume no error and nothing done.
- theErr = noErr;
- data->action = kDidNothing;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- GetDialogItem((DialogPtr)pStorage->port, pStorage->baseItem + data->itemHit, &iType, &theItem, &iBox);
-
- switch (data->itemHit)
- {
- case iOrigColor:
- theErr = DoListClick(storage, data);
- break;
-
- case iPrevPict:
- resFile = OpenScrapBookFile();
- require(resFile > 0, bail);
- (*storage)->numPicts = Count1Resources('PICT');
- CloseResFile(resFile);
-
- bail:
-
- (*storage)->pictNumber--;
- if ((*storage)->pictNumber < -1)
- (*storage)->pictNumber = (*storage)->numPicts;
- DrawColorEditor(storage, kDrawPict);
- break;
-
- case iNextPict:
- resFile = OpenScrapBookFile();
- require(resFile > 0, bail2);
- (*storage)->numPicts = Count1Resources('PICT');
- CloseResFile(resFile);
-
- bail2:
-
- (*storage)->pictNumber++;
- if ((*storage)->pictNumber > (*storage)->numPicts)
- (*storage)->pictNumber = -1;
- DrawColorEditor(storage, kDrawPict);
- break;
-
- default:
- break;
- }
-
- HSetState((Handle)storage, wasState);
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetBaseItem
-
- pascal ComponentResult doPickerSetBaseItem (PickerStorageHandle storage, SInt16 baseItem)
- {
- (*storage)->baseItem = baseItem;
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerGetProfile
-
- pascal ComponentResult doPickerGetProfile (PickerStorageHandle storage)
- {
- #pragma unused (storage)
-
- return 0L;
- }
-
- //--------------------------------------------------------------------- doPickerSetProfile
-
- pascal ComponentResult doPickerSetProfile (PickerStorageHandle storage, CMProfileHandle profile)
- {
- #pragma unused (storage, profile)
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerGetPrompt
-
- pascal ComponentResult doPickerGetPrompt (PickerStorageHandle storage, Str255 prompt)
- {
- PasStringCopy((*storage)->prompt, prompt);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetPrompt
-
- pascal ComponentResult doPickerSetPrompt (PickerStorageHandle storage, Str255 prompt)
- {
- PasStringCopy(prompt, (*storage)->prompt);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerGetIconData
-
- pascal ComponentResult doPickerGetIconData (PickerStorageHandle storage, PickerIconData* data)
- {
- PickerIconData** pickerData;
- OSErr theErr;
- SInt16 fileRef;
-
- // Set up some default values.
- theErr = noErr;
- data->scriptCode = 0;
- data->iconSuiteID = kPickerData;
-
- // Open the components res fork.
- fileRef = OpenComponentResFile((Component)(*storage)->myself);
- require(fileRef > 0, fail);
-
- // Get the picker data out.
- pickerData = (PickerIconData **)HappiGetResource(kPickerDataType, kPickerData, &theErr);
- require_action(pickerData, fail, (void) CloseComponentResFile(fileRef););
-
- // Copy the data to our struct and dispose of the resource handle.
- *data = **pickerData;
- DisposeHandle((Handle)pickerData);
-
- // Close res fork again.
- theErr = CloseComponentResFile(fileRef);
- check(theErr == noErr);
-
- return theErr;
-
- fail:
-
- return pickerResourceError;
- }
-
- //--------------------------------------------------------------------- doPickerGetEditMenuState
-
- pascal ComponentResult doPickerGetEditMenuState (PickerStorageHandle storage,
- PickerMenuState* mState)
- {
- #pragma unused (storage)
-
- SInt32 scrapOffset;
-
- mState->cutEnabled = true;
- mState->copyEnabled = true;
- (void) TEToScrap();
- mState->pasteEnabled = (GetScrap(0L, 'TEXT', &scrapOffset) > 0L);
- mState->clearEnabled = true;
- // mState->undoEnabled = (*storage)->dirty;
-
- GetIndString(mState->undoString, kPickerMiscStrsID, kUndoStringIndex);
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetOrigin
-
- pascal ComponentResult doPickerSetOrigin (PickerStorageHandle storage, Point where)
- {
- #pragma unused (storage, where)
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerExtractHelpItem
-
- pascal ComponentResult doPickerExtractHelpItem (PickerStorageHandle storage, SInt16 itemNo,
- SInt16 whichState, HelpItemInfo* helpInfo)
- {
- OSErr theErr, wasErr;
- SInt16 resFile, count;
-
- theErr = noHelpForItem;
- wasErr = noErr;
-
- itemNo -= (*storage)->baseItem;
-
- resFile = OpenComponentResFile((Component)(*storage)->myself);
- require(resFile > 0, bail);
-
- theErr = HMGetIndHelpMsg(kHMDialogResType, kPickerHelpID, itemNo, whichState,
- (UInt32 *)&helpInfo->options, &helpInfo->tip,
- &helpInfo->altRect, &helpInfo->theProc, &helpInfo->helpVariant,
- &helpInfo->helpMessage, &count);
-
- // If item skipped for balloon help, report back as noHelpForItem.
- if (theErr == hmSkippedBalloon)
- theErr = noHelpForItem;
-
- check((theErr == noErr) || (theErr == noHelpForItem));
-
- if (theErr != noErr)
- wasErr = theErr;
- theErr = CloseComponentResFile(resFile);
- check(theErr == noErr);
-
- if ((theErr == noErr) && (wasErr != noErr))
- theErr = wasErr;
-
- bail:
-
- return theErr;
- }
-
- #if NEW_COLORPICKER_2_1_CALLS
-
- //--------------------------------------------------------------------- doPickerSetColorChangedProc
-
- pascal ComponentResult doPickerSetColorChangedProc (PickerStorageHandle storage, ColorChangedUPP colorProc, long colorProcData)
- {
- (*storage)->colorProc = colorProc;
- (*storage)->colorProcData = colorProcData;
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doNPickerGetColor
-
- pascal ComponentResult doNPickerGetColor (PickerStorageHandle storage, ColorType whichColor, NPMColorPtr color)
- {
- PickerStoragePtr pStorage;
- char wasState;
- OSErr theErr;
-
- // Check we were called through new API
- if (!(*storage)->calledNPickColor
- return cantLoadPicker;
-
- theErr = noErr;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- if (whichColor == kNewColor)
- {
- *color = pStorage->currNPMColor;
-
- // If user changed something…
- if (pStorage->dirty)
- {
- color->color.rgb = pStorage->currColor;
- PickerMatchColors( &(pStorage->PickerToNColorCW), &(color->color), 1);
- }
- }
- else
- *color = pStorage->origNPMColor;
-
- HSetState((Handle)storage, wasState);
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- doNPickerSetColor
-
- pascal ComponentResult doNPickerSetColor (PickerStorageHandle storage, ColorType whichColor, NPMColorPtr color)
- {
- CMPickerColor undoColor;
-
- // Remember we were called through new API
- (*storage)->calledNPickColor = true;
-
- if (whichColor == kNewColor)
- {
- undoColor = (*storage)->currColor;
- (*storage)->currNPMColor = *color;
- }
- else
- (*storage)->origNPMColor = *color;
-
- // If the picker is already visible, we need to update everything.
- if ((*storage)->visible)
- {
- PickerStoragePtr pStorage;
- CMColor tempColor;
- char wasState;
-
- // Lock and point to storage.
- wasState = HGetState((Handle)storage);
- HLock((Handle)storage);
- pStorage = *storage;
-
- if (whichColor == kNewColor)
- {
- tempColor = pStorage->currNPMColor.color;
- PickerMatchColors( &(pStorage->NColorToPickerCW), &tempColor, 1);
- pStorage->undoColor = pStorage->currColor = tempColor.rgb;
- GetUserValsFromColor((CMColor*)&pStorage->currColor, (CMColor*)&pStorage->currUserColor,
- kUserColorSpace);
-
- // The color just lost moves into undo.
- pStorage->undoColor = undoColor;
-
- // Now update everything.
- DrawColorEditor(storage, kDrawEverything);
-
- pStorage->wasUserColor = pStorage->currUserColor;
- }
- else
- {
- tempColor = pStorage->origNPMColor.color;
- #if ASSUME_CURR_ORIG_PROFS_SAME
- PickerMatchColors( &(pStorage->NColorToPickerCW), &tempColor, 1);
- #else
- PickerMatchColors( &(pStorage->OColorToPickerCW), &tempColor, 1);
- #endif
- pStorage->origColor = tempColor.rgb;
- GetUserValsFromColor((CMColor*)&pStorage->origColor,
- (CMColor*)&pStorage->origUserColor, kUserColorSpace);
-
- // Update the internal pattern.
- SetColorPattern(storage, kOriginalColor);
-
- // Now update just the list.
- DrawColorEditor(storage, kDrawNew+kDrawOrig);
- }
-
- HSetState((Handle)storage, wasState);
- }
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doNPickerGetProfile
-
- pascal ComponentResult doNPickerGetProfile (PickerStorageHandle storage, CMProfileRef *profile)
- {
- // Check we were called through new API
- if (!(*storage)->calledNPickColor
- return cantLoadPicker;
-
- // Return the stored the ProfileLocation Ptr.
- *profile = (*storage)->profileRef;
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doNPickerSetProfile
-
- pascal ComponentResult doNPickerSetProfile (PickerStorageHandle storage, CMProfileRef profile)
- {
- // Remember we were called through new API
- (*storage)->calledNPickColor = true;
-
- // Store the ProfileLocation Ptr.
- (*storage)->profileRef = profile;
-
- return noErr;
- }
-
- //--------------------------------------------------------------------- doPickerSetColorChangedProc
-
- pascal ComponentResult doNPickerSetColorChangedProc (PickerStorageHandle storage, NColorChangedUPP colorProc, long colorProcData)
- {
- (*storage)->ncolorProc = colorProc;
- (*storage)->colorProcData = colorProcData;
-
- return noErr;
- }
-
- #endif // NEW_COLORPICKER_2_1_CALLS
-
-
-